<template>
  <div class="app-container">
    <!--    <PackageFunctionText title="站点" identifying="number_sites" />-->
    <div class="filter-container">
      <el-input v-model="listQuery.name" :placeholder="$t('common.name')" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-if="user.standing==2" v-model="listQuery.data_type" :placeholder="$t('common.dataType')" class="filter-item" @change="handleDataTypeFilter">
        <el-option
          v-for="item in dataTypeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('common.search') }}
      </el-button>

      <router-link v-if="user.standing==2" to="/site/add">
        <el-button plain class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus">
          {{ $t('site.addInternalWebsite') }}
        </el-button>
      </router-link>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="onAddSiteUrl">
        {{ $t('site.goToCreateAWebsite') }}
      </el-button>

    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="180">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('common.cover')" width="120">
        <template slot-scope="{row}">
          <el-image :src="row.avatar" fit="cover" style="width: 50px;height: 50px" :preview-src-list="[row.avatar]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" style="font-size: 40px" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('common.name')">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
          <div v-if="user.standing==2">
            <el-tag v-if="row.data_type==1" type="success">{{ row.data_type_text }}</el-tag>
            <el-tag v-if="row.data_type==2">{{ row.data_type_text }}</el-tag>

            <el-tag v-if="row.is_successful_case==0" type="info">{{ row.successful_case_text }}</el-tag>
            <el-tag v-if="row.is_successful_case==1" type="success">{{ row.successful_case_text }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" :label="$t('common.status')" width="110">
        <template slot-scope="{row}">
          <el-tag v-if="row.status==0" type="info">{{ row.status_text }}</el-tag>
          <el-tag v-if="row.status==1" type="success">{{ row.status_text }}</el-tag>

        </template>
      </el-table-column>

      <el-table-column :label="$t('common.creationTime')" width="160">
        <template slot-scope="{row}">
          <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" :label="$t('common.updateTime')" width="160">
        <template slot-scope="{row}">
          <span>{{ row.update_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('common.operate')" width="200">
        <template slot-scope="scope">
          <el-link :href="getSiteAdminUrl(scope.row.id)" target="_blank">
            <el-button type="primary" plain size="mini" icon="el-icon-setting">
              {{ $t('common.manage') }}
            </el-button>
          </el-link>
          <router-link :to="'/site/edit/'+scope.row.id">
            <el-button size="mini" icon="el-icon-edit">
              {{ $t('common.edit') }}
            </el-button>
          </router-link>
          <!--          <router-link v-if="user.standing==2" :to="'/site/function-combination/'+scope.row.id">-->
          <!--            <el-button size="small" icon="el-icon-edit">-->
          <!--              功能组合-->
          <!--            </el-button>-->
          <!--          </router-link>-->
          <router-link :to="'/site/domain-name/'+scope.row.id">
            <el-button size="mini" icon="el-icon-s-tools">
              {{ $t('common.domainNameManagement') }}
            </el-button>
          </router-link>

          <el-popconfirm
            :title="$t('common.areYouSureToDeleteTheContent')"
            @onConfirm="confirmDelete(scope.row)"
          >
            <el-button slot="reference" size="mini" icon="el-icon-delete">{{ $t('common.recycleBin') }}</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="getList" />
  </div>
</template>

<script>
import { getSiteList, remove } from '@/api/site'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import store from '@/store'
// import PackageFunctionText from '@/components/PackageFunctionText'
import { getLanguage } from '@/utils/cookies'

export default {
  name: 'SiteIndex',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 10,
        title: undefined,
        data_type: undefined
      },
      dataTypeOptions: [{
        value: 1,
        label: this.$t('site.websiteData')
      }, {
        value: 2,
        label: this.$t('site.templateData')
      }],
      user: {}
    }
  },
  created() {
    this.getUser()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getSiteList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getSiteAdminUrl(siteId) {
      return process.env.VUE_APP_SITE_ADMIN_URL + '?site_id=' + siteId + '&lang=' + getLanguage() + '&token=' + store.getters.token
    },
    handleDataTypeFilter(val) {
      this.listQuery.data_type = val
      this.handleFilter()
    },
    getUser() {
      this.$store.dispatch('user/getInfo').then(res => {
        this.user = res
      })
    },
    onAddSiteUrl() {
      window.open(process.env.VUE_APP_WEB_URL + '/w-t')
    },
    confirmDelete(value) {
      var _this = this
      remove(value.id).then(res => {
        this.$notify({
          message: res.message,
          type: 'success',
          duration: 1 * 1000,
          onClose() {
            _this.listQuery.page = 1
            _this.getList()
          }
        })
      })
    }
  }
}
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
